package com.wisdom;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bluecall.sipua.R;
import com.wisdom.tools.CCPConfig;
import com.wisdom.voip.CallOutActivity;
import com.wisdom.voip.VoiceHelper;

public class DialActivity extends Activity implements OnClickListener{
	private static final int VOIP_CALL_TYPE_BACK_TALK = 203;
	private static final int VOIP_CALL_TYPE_DIRECT_TALK = 204;
	
	private EditText mOutPhoneNumEt;
	private EditText mPhoneNum;
	
	private Button btn_getMute;
	private Button mDirectCallBtn;
	private Button mCallBackBtn;
	private Button mGetStateBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dial);
		
		initResourceRefs();
	}
	
	private void initResourceRefs(){
		
		mOutPhoneNumEt = (EditText) findViewById(R.id.outphone_number_input);
		mPhoneNum = (EditText) findViewById(R.id.editTextCode);
		
       mDirectCallBtn = (Button) findViewById(R.id.netphone_direct_call);
       mCallBackBtn = (Button) findViewById(R.id.netphone_call_back);
       mGetStateBtn = (Button) findViewById(R.id.btn_getState);
       btn_getMute = (Button) findViewById(R.id.btn_getMute);
       
       mDirectCallBtn.setOnClickListener(this);
		mCallBackBtn.setOnClickListener(this);
		mGetStateBtn.setOnClickListener(this);
		btn_getMute.setOnClickListener(this);
		mPhoneNum.setText(CCPConfig.Src_phone);
	}
	@Override
	public void onClick(View v){
		switch(v.getId()){
		case R.id.netphone_direct_call:
			//设置用户输入信息，跳转页面
			CCPConfig.Src_phone=mPhoneNum.getText().toString();
			VoiceApplication.getInstance().getVoiceHelper().getDevice().setSelfPhoneNumber(CCPConfig.Src_phone);
			startVoipCall(VOIP_CALL_TYPE_DIRECT_TALK);
			break;
		case R.id.netphone_call_back:
			CCPConfig.Src_phone = mPhoneNum.getText().toString();
			VoiceApplication.getInstance().getVoiceHelper().getDevice().setSelfPhoneNumber(CCPConfig.Src_phone);
			startVoipCall(VOIP_CALL_TYPE_BACK_TALK);
			break;
		case R.id.btn_getState:
			getState();
			break;
		case R.id.btn_openlog:
			openLog();
			break;
		case R.id.btn_getMute:
			getMute();
			break;
		default:
			break;
		}
	}
	void getMute(){
		String ff=String.valueOf(VoiceApplication.getInstance().getVoiceHelper().getDevice().getMuteStatus());
		Toast.makeText(getApplicationContext(), ff, Toast.LENGTH_SHORT).show();

	}
	void openLog(){
		VoiceApplication.getInstance().getVoiceHelper().getDevice().enableLog(true);
		Toast.makeText(getApplicationContext(), "云通讯成功启用日志！", Toast.LENGTH_SHORT).show();
	}
	void startVoipCall(int mCallType){
		String phoneStr=mOutPhoneNumEt.getText().toString();
		
		if(TextUtils.isEmpty(phoneStr)){
			VoiceApplication.getInstance().showToast(R.string.edit_input_empty);
		}
		
		Intent intent=new Intent(this,CallOutActivity.class);
		//需要根据模式传递相应参数
		switch(mCallType){
		case VOIP_CALL_TYPE_DIRECT_TALK:
			//直播电话
			if(!phoneStr.startsWith("0")&&!phoneStr.startsWith("1")){
				//判断输入合法性
				VoiceApplication.getInstance().showToast(getString(R.string.network_dial_number_format));
				return;
			}
			intent.putExtra(VoiceApplication.VALUE_DIAL_VOIP_INPUT,phoneStr);
			intent.putExtra(VoiceApplication.VALUE_DIAL_MODE,VoiceApplication.VALUE_DIAL_MODE_DIRECT);
			startActivity(intent);
			break;
		case VOIP_CALL_TYPE_BACK_TALK:
	     	//回拨电话
			if(!phoneStr.startsWith("0")&&!phoneStr.startsWith("1")){
				//判断输入合法性
				VoiceApplication.getInstance().showToast(getString(R.string.network_dial_number_format));
				return;
			}
			if(CCPConfig.Src_phone==null||CCPConfig.Src_phone.equals("")){
				//判断本机号码是否正确
				VoiceApplication.getInstance().showToast(getString(R.string.src_phone_empty));
				return;
				
			}
			intent.putExtra(VoiceApplication.VALUE_DIAL_VOIP_INPUT,phoneStr);
			intent.putExtra(VoiceApplication.VALUE_DIAL_MODE, VoiceApplication.VALUE_DIAL_MODE_BACK);
			startActivity(intent);
			
		}
	}
	void getState(){
		
		VoiceHelper voiceHelper=VoiceApplication.getInstance().getVoiceHelper();
		if(voiceHelper!=null&&voiceHelper.getDevice()!=null){
			Toast.makeText(getApplicationContext(), voiceHelper.getDevice().isOnline().toString(), Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(),"VoiceHelper 为空", Toast.LENGTH_SHORT).show();
		}
	}
	
	
}
